package com.hanhai.zrb.backend.web.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Maps;
import com.hanhai.zrb.api.biz.service.IndustryService;
import com.hanhai.zrb.model.project.Industry;
import com.hanhai.zrb.model.project.dto.IndustryDto;

@Controller
@RequestMapping("industry")
public class IndustryController {
	@Resource
	private IndustryService industryService;
	
	@RequestMapping("getIndustryNameById")
	public void getIndustryNameById(int id, HttpServletResponse response)
			throws IOException {
		Industry industry = industryService.selectByPrimaryKey(id);
		Map<String, Object> map = new HashMap<String, Object>();
		String industryName = "";
		if (null != industry) {
			industryName = industry.getIndustryName();
		}
		map.put("industryName", industryName);
		String str = JSONObject.toJSONString(map);
		response.getWriter().write(str);
	}

	
	@RequestMapping("queryAll")
	public void queryAll(HttpServletResponse response) throws IOException {
		List<Industry> industryList = industryService.selectAll();
		String str = JSONObject.toJSONString(industryList);
		response.getWriter().write(str);
	}
	
	@RequestMapping("queryAllForProject")
	public void queryAllForProject(HttpServletResponse response) throws IOException {
		List<Industry> industryList = industryService.selectAll();
		for(Industry i:industryList){
			if(i.getId() == 97){
				industryList.remove(i);
				break;
			}
		}
		String str = JSONObject.toJSONString(industryList);
		response.getWriter().write(str);
	}
	@RequestMapping("querySquAll")
	public void querySquAll(HttpServletResponse response) throws IOException {
		List<Industry> industryList = industryService.selectSquAll();
		String str = JSONObject.toJSONString(industryList);
		response.getWriter().write(str);
	}
	
	
	@RequestMapping("index")
	public String index() {
		return "industry/queryList";
	}
	
   /**
    * 
    * @Title: queryList 
    * @Description: TODO(分页查询所有行业信息) 
    * @param @param request
    * @param @param response
    * @param @param model
    * @param @throws IOException    设定文件 
    * @return void(返回类型 ) 
    * @throws
    */
	@RequestMapping("queryList")
	public void queryList(String industryName,HttpServletRequest request, HttpServletResponse response) throws IOException {
		int page = 1;
		int rows = 1;
		IndustryDto industryDto = new IndustryDto();

		if(null!=request.getParameter("page")){
			page = Integer.parseInt(request.getParameter("page"));
		}
		if(null!=request.getParameter("rows")){
			rows = Integer.parseInt(request.getParameter("rows"));
		}
		industryDto.setPageSize(rows);
		industryDto.setPageNum(page);

		industryDto.setSearchName(industryName);
		PageInfo pageInfo =  industryService.queryPageByCondition(industryDto);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("rows", pageInfo.getList()); 
		map.put("total", pageInfo.getTotal());
		String str = JSONObject.toJSONString(map);
		response.getWriter().write(str);
	}
	
	
	@RequestMapping(value="save")
	public void save(String industryName,String squences,String color,HttpServletResponse response) throws Exception{
		Map<String, Object> map=new HashMap<String, Object>();
		Industry industry = new Industry();
        if(StringUtils.isNotBlank(industryName.trim())){
        	industry.setIndustryName(industryName);
        	industry.setSquences(StringUtils.isNumeric(squences)?Integer.valueOf(squences):null);
        	industry.setIndustryLevel(1);
        	industry.setTopIndustryId(0);
        	industry.setIshidden(1);
        	industry.setColor(color);
        	industry.setUpdateTime(new Date());
    		Integer id = industryService.checkIndustryName(industryName);
    		if(null!=id){
    			map.put("success", false);
    			map.put("infoMsg", "行业已存在");
    		}else {
    			try {
    				industryService.insert(industry);
    				map.put("success", true);
    				map.put("infoMsg", "行业添加成功");
				} catch (Exception e) {
    				map.put("success", false);
    				map.put("errorMsg", "行业添加失败");
				}
    		}
        }else{
			map.put("success", true);
			map.put("errorMsg", "政府行业不能为空");
        }
		
		String str=JSONObject.toJSONString(map);
		response.getWriter().write(str);
	}

	
	
	@RequestMapping(value="edit")
	public void edit(String ids,String industryNames,String squencesStr,String colorStr,HttpServletResponse response) throws Exception{
		Map<String, Object> map=new HashMap<String, Object>();
		List<Industry> list = new ArrayList();
		String[] idArray = ids.split(",");
		String[] namesArray = industryNames.split(",");
		String[] squenceArray = squencesStr.split(",");
		String[] colorArray = colorStr.split(",");
		for(int i=0;i<idArray.length;i++){
			Industry industry = new Industry();
			industry.setId(Integer.valueOf(idArray[i]));
			industry.setIndustryName(namesArray[i]);
			industry.setColor(colorArray[i]);
			if(squenceArray.length>i){
				industry.setSquences(StringUtils.isNumeric(squenceArray[i])?Integer.valueOf(squenceArray[i]):null);
			}
//		Integer id = industryService.checkIndustryName(namesArray[i]);
//			if(null!=id){
//				map.put("success", false);
//				map.put("infoMsg", "行业已经存在");
//			}else{
				try {
					industryService.updateByPrimaryKeySelective(industry);
					map.put("success", true);
					map.put("infoMsg", "行业修改成功");
				} catch (Exception e) {
					map.put("success", false);
					map.put("errorMsg", "行业修改失败");
				}

//			}

		}
		String str=JSONObject.toJSONString(map);
		response.getWriter().write(str);
	}
	
	
	
	//更新状态
	
	//更改会员状态
	@RequestMapping("/updateStatus")
	@ResponseBody
	public String updateStatus(String ids ,String status){
		boolean ret = true ;
		Map<String, Object> map = Maps.newHashMap();
	   	if(StringUtils.isNotBlank(ids) && StringUtils.isNotBlank(status)){
	   		List<Long> idList = new ArrayList();
			String[] str = ids.split(",");
			for (int i = 0; i < str.length; i++) {
				if(StringUtils.isNumeric(str[i])){
					idList.add(Long.valueOf(str[i]));
				}
			}
	   		ret = industryService.upageStatus(idList, status);
	   	}else{
	   		ret = false;
	   	}
		if(ret){
			map.put("success", true);
			map.put("infoMsg", "修改状态成功");
		}else{
			map.put("success", false);
			map.put("errorMsg", "修改状态失败,请稍后再试");
		}   	
	   	return JSON.toJSONString(map);
	}
	
	

}
